import _MessageBox from './MessageBox';

// export default _MessageBox;

export default {
  install(Vue) {
    let messageBox = null;

    Vue.component(_MessageBox.name, _MessageBox);

    Vue.prototype.$messageBox = {
      show,
      hide,
      info({ title, content }, callback) {
        this.show({ title, content, type: 'primary' }, callback);
      },
      success({ title, content }, callback) {
        this.show({ title, content, type: 'success' }, callback);
      },
      warning({ title, content }, callback) {
        this.show({ title, content, type: 'warning' }, callback);
      },
      danger({ title, content }, callback) {
        this.show({ title, content, type: 'danger' }, callback);
      }
    }

    function show(props, callback) {
      if (!messageBox) {
        const MessageBox = Vue.extend({
          render(h) {
            return h('message-box', {
              props: {...props}
            })
          }
        })

        messageBox = new MessageBox();

        this.vm = messageBox.$mount();
        document.body.appendChild(this.vm.$el);

        callback && callback();
      }
    }

    function hide(callback) {
      document.body.removeChild(this.vm.$el);
      messageBox.$destroy();
      messageBox = null;
      this.vm = null;
      callback && callback();
    }
  }
}